package com.gjzt.business.media.service;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.StringUtil;
import com.gjzt.business.media.inter.MediaLineMapper;
import com.gjzt.business.media.model.MediaLine;

@Service
public class MediaLineService extends BaseService {
	
	@Autowired
    private MediaResourceMarksService mediaResourceMarksService;

    @Autowired
    private MediaLineMapper mediaLineMapper;
    
    @Autowired
	private WuhanbusAPIService wuhanbusAPIService;


    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject findMediaLine(ReqBean reqBean) {
    	StringBuffer sql = new StringBuffer("select * from MEDIA_LINE t where 1 = 1 ");
        if(!StringUtil.isEmpty(reqBean.getInfo("lineName"))){
        	sql.append(" and t.LINE_NAME like '").append(reqBean.getInfo("lineName")).append("%'");
        }
        if(!StringUtil.isEmpty(reqBean.getInfo("mediaLevel"))){
        	sql.append(" and t.MEDIA_LEVEL = '").append(reqBean.getInfo("mediaLevel")).append("'");
        }
        
        sql.append(" order by t.LINE_NAME");
        return this.queryTable(sql.toString(),null,reqBean.getPageSize(), reqBean.getPageIndex());
    }
    
    /**
     * 保存
     *
     * @param reqBean
     */
    public void saveMediaLine(ReqBean reqBean) {
    	String lineId = reqBean.getInfo("lineId");
    	String lineName = reqBean.getInfo("lineName");
    	String gpsCode = reqBean.getInfo("gpsCode");
    	String deptId = reqBean.getInfo("deptId");
    	String deptName = reqBean.getInfo("deptName");
    	String vehCount = reqBean.getInfo("vehCount");
    	String mediaLevel = reqBean.getInfo("mediaLevel");
        Assert.hasLength(lineId);
        
        MediaLine line = new MediaLine();
        line.setLineId(lineId);
        line.setLineName(lineName);
        line.setGpsCode(gpsCode);
        line.setDeptId(deptId);
        line.setDeptName(deptName);
        line.setVehCount(vehCount);
        line.setMediaLevel(mediaLevel);
        mediaLineMapper.updateMediaLine(line);
    	
    	if(reqBean.containsKey("mediaMarks")){
    		mediaResourceMarksService.removeMarksOfLine(lineId);
    		String markIdsStr = reqBean.getInfo("mediaMarks");
    		JSONArray markIds = JSONArray.fromObject(markIdsStr);
    		for(Object markId : markIds){
    			mediaResourceMarksService.createLineMark(markId.toString(), lineId);
    		}
    	}
    }
    
    /**
     * 同步线路数据
     */
    public synchronized void syncLines(){
    	JSONArray lines = wuhanbusAPIService.getLines();
    	if(lines == null || lines.isEmpty()){
    		return;
    	}
    	
    	StringBuffer sql = new StringBuffer("select * from MEDIA_LINE ");
    	JSONArray dbLines = this.query(sql.toString());
    	Map<String, JSONObject> dbMaps = new HashMap<String, JSONObject>(); 
    	if(dbLines != null && !dbLines.isEmpty()){
    		for(int i = 0 ;i < dbLines.size(); i++){
        		JSONObject dbline = dbLines.getJSONObject(i);
        		dbMaps.put(dbline.getString("lineId"), dbline);
        	}
    	}
    	
    	String _sqlUpdate = "update MEDIA_LINE set LINE_NAME = ?,GPS_CODE = ?,DEPT_ID = ?,DEPT_NAME = ?, VEH_COUNT = ? WHERE LINE_ID = ?";
    	String _sqlInsert = "insert into MEDIA_LINE(LINE_ID,LINE_NAME,GPS_CODE,VEH_COUNT,DEPT_ID,DEPT_NAME) VALUES(?,?,?,?,?,?)";
    	
    	String id, name,gps_code, veh_count,dept_id,dept_name;
    	for(int i = 0 ;i < lines.size(); i++){
    		JSONObject _line = lines.getJSONObject(i);
    		id = StringUtil.noNull(_line.getString("id"));
    		name = StringUtil.noNull(_line.getString("name"));
    		gps_code = StringUtil.noNull(_line.getString("gps_code"));
    		veh_count = StringUtil.noNull(_line.getString("veh_count"));
    		dept_id = StringUtil.noNull(_line.containsKey("dept_id") ? _line.getString("dept_id") : "");
    		dept_name = StringUtil.noNull(_line.getString("dept_name"));
    		
    		JSONObject dbLine = dbMaps.remove(id);
    		if(dbLine !=  null){
    			if(!name.equals(dbLine.getString("lineName"))
    					|| !gps_code.equals(dbLine.getString("gpsCode"))
    					|| !dept_id.equals(dbLine.getString("deptId"))
    					|| !dept_name.equals(dbLine.getString("deptName"))
    					|| !veh_count.equals(dbLine.getString("vehCount"))){//信息变更
    				this.update(_sqlUpdate, name, gps_code,dept_id,dept_name, veh_count, id);
    			}
    		}else{
    			this.update(_sqlInsert, id,name,gps_code, veh_count,dept_id,dept_name);
    		}
    	}
    	
//    	for (Map.Entry<String, JSONObject> entry : dbMaps.entrySet()) {//数据删除
//    		JSONObject line = entry.getValue();
//    		
//    	}
    	
    }

    
}